/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ru.bfpro.wot.stat;

import java.io.StringReader;
import javax.json.*;

/**
 *
 * @author fbn
 */
public class StatValues {
    private JsonObject data;
    private JsonObject achievements;
    private JsonObject ratings;
    private JsonArray vehicles;
    private JsonObject battles;
    private JsonObject summary;
    private JsonObject experience;
    private JsonObject clan;
    private JsonObject statistics;
    private int timestamp;

    private final int OLD_FORMAT = 1382124542;

    public JsonObject getData() {
        return data;
    }

    public void setData(JsonObject data) {
        this.data = data;
    }

    public StatValues(String in) {
        JsonReader jr = Json.createReader(new StringReader(in));
        data = jr.readObject();
        timestamp = data.getInt("updated_at");  // До 1382124542 - старый формат
        // clan = data.getJsonObject("clan");
        achievements = data.getJsonObject("achievements");

        if (this.timestamp >= OLD_FORMAT) {
            statistics = data.getJsonObject("statistics").getJsonObject("all");
        }
        ratings = data.getJsonObject("ratings");
        vehicles = data.getJsonArray("vehicles");
        battles = data.getJsonObject("battles");
        summary = data.getJsonObject("summary");
        experience = data.getJsonObject("experience");
    }

    public int wins() {
        if (this.timestamp < OLD_FORMAT) {
            return summary.getInt("wins");
        } else {
            return statistics.getInt("wins");
        }
    }

    public int battles_count() {
        if (this.timestamp < OLD_FORMAT) {
            return summary.getInt("battles_count");
        } else {
            return statistics.getInt("battles");
        }
    }

    public int avg_xp() {
        if (this.timestamp < OLD_FORMAT) {
            return experience.getInt("battle_avg_xp");
        } else {
            return statistics.getInt("battle_avg_xp");
        }
    }

    public float winPrc() {
        return (float)wins()/battles_count()*100;
    }

    public int survived_battles() {
        if (this.timestamp < OLD_FORMAT) {
            return summary.getInt("survived_battles");
        } else {
            return statistics.getInt("survived_battles");
        }
    }

    public float survivedPrc() {
        return (float)survived_battles()/battles_count()*100;
    }

    public int destroyed() {
        if (this.timestamp < OLD_FORMAT) {
            return battles.getInt("frags");
        } else {
            return statistics.getInt("frags");
        }
    }

    public float destroyedPrc() {
        return (float)destroyed()/ (float)battles_count();
    }

    public int beLost() {
        return battles_count() - survived_battles();
    }

    public float killMeter() {
        return (float)destroyed()/(float)beLost();
    }

    public int damage_dealt() {
        if (this.timestamp < OLD_FORMAT) {
            return ratings.getJsonObject("damage_dealt").getInt("value");
        } else {
            return statistics.getInt("damage_dealt");
        }
    }

    public int damage_received() {
        if (this.timestamp < OLD_FORMAT) {
            return 0;
        } else {
            return statistics.getInt("damage_received");
        }
    }

    public float avg_damage() {
        return (float)damage_dealt()/(float)battles_count();
    }

    public float avg_damage_received() {
        return (float)damage_received()/(float)battles_count();
    }

    public float damageMeter() {
        if (this.timestamp < OLD_FORMAT) {
            return 0;
        } else {
            return (float)damage_dealt()/(float)damage_received();
        }
    }

    public int hits() {
        if (this.timestamp < OLD_FORMAT) {
            return battles_count()*hitsPrc()/10;
        } else {
            return statistics.getInt("hits");
        }
    }

    public int shots() {
        return statistics.getInt("shots");
    }

    public int hitsPrc() {
        if (this.timestamp < OLD_FORMAT) {
            return battles.getInt("hits_percents");
        } else {
            return statistics.getInt("hits_percents");
        }

    }

    public float avg_hits() {
        if (this.timestamp < OLD_FORMAT) {
            return 0;
        } else {
            return (float)(hits())/(float)(battles_count());
        }
    }

    public float avg_shots() {
        if (this.timestamp < OLD_FORMAT) {
            return 0;
        } else {
            return (float)(shots())/(float)(battles_count());
        }
    }

    public float avg_shotDamage() {
        if (this.timestamp < OLD_FORMAT) {
            return 0;
        } else {
            return (float)(damage_dealt())/(float)(shots());
        }
    }

    public float avg_hitDamage() {
        return (float)(damage_dealt())/(float)(hits());
    }

}
